package com.example.educationblockchain.controller;

import com.example.educationblockchain.model.Data;
import com.example.educationblockchain.service.P2PService;
import com.example.educationblockchain.service.StudentInformationService;
import com.example.educationblockchain.util.SseEmitterServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/studentInfo")
public class StudentInformationController {

    @Autowired
    StudentInformationService studentInformationService;

    @Autowired
    SseEmitterServer sseEmitterServer;

    @Autowired
    P2PService p2PService;

    @GetMapping("/getDetail")
    @ResponseBody
    public Map<String,Object> getDetail() throws BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {
        Map map = studentInformationService.getData();
        return map;
    }

    @GetMapping("/getDataByPage")
    @ResponseBody
    public Map<String,Object> getDataByPage(int page) throws BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {
        Map map = studentInformationService.getDataByPage(page);
        return map;
    }

    @PostMapping("/changeInfo")
    @ResponseBody
    public long changeInfo(@RequestBody Map<String,Object> data) throws BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {

        System.out.println(data);

        studentInformationService.changeStudentInfo(data);

        long index = 0;

        return index;
    }

    @GetMapping("TT1/{id}")
    public SseEmitter getSseEmitter(@PathVariable Long id) throws IOException, InterruptedException {
        //0L设置永不超时

        return sseEmitterServer.connect(id+"");
    }

    @GetMapping("getPeerData/{id}")
    public SseEmitter getPeerData(@PathVariable String id) throws IOException, InterruptedException {
        //0L设置永不超时

        return sseEmitterServer.connect(id);
    }

    @PostMapping("/changePassword")
    @ResponseBody
    public String changePassword(@RequestBody Map<String,String> map){
        System.out.println(map);

        return studentInformationService.changePassword(map);

    }

    @PostMapping("/sseSent")
    @ResponseBody
    public String sseSent(@RequestBody Map<String,String> data) throws BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {

        System.out.println(data);

        p2PService.SseSend(data);

        return "";

    }


    //这个方法仅仅是测试，可根据业务来推送
    @PostMapping("TT2/{id}")
    public void test(@PathVariable Long id) throws IOException, InterruptedException {
        //每3s向前端推送消息
        /*
        for (int i = 0; i < 10; i++) {
            sseEmitterServer.sendMessage(id+"","第"+i+"条信息");
            Thread.sleep(3000);
        }
        sseEmitterServer.removeUser(id+"");

         */
    }

}
